<?php
 
 
include '../c/config.php';

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, 'klnm');
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
 

function trimall($str) 
{
    $qian=array(" ","　","\t","\n","\r");
    $hou=array("","","","","");
    return str_replace($qian,$hou,$str); 
}
 
$sql="SELECT openid FROM `download` WHERE `idcard` IS NULL   ORDER BY `id`  DESC LIMIT 0,1 ";
$query =$mysqli->query($sql);
if($query->num_rows>0){
    $list=$query->fetch_array();

     $cardNo='';
        $queryCard=$mysqli->query("SELECT msgtxt FROM `httplog` WHERE `openid`='".$list['openid']."' limit 0,1 ");
        if($queryCard->num_rows>0){
            $getCard=$queryCard->fetch_array();
            $getTxt=trim($getCard['msgtxt']); 
            $getTxt=trimall($getTxt);
            $getTxt2=json_decode($getTxt, true);
            if (isset($getTxt2['user'])) {
             	$cardNo=$getTxt2['user']['card_number'];
             }
             if (isset($getTxt2['card_number'])) {
             	$cardNo=$getTxt2['card_number'];
             }
        }else{
            $queryCard=null;
            $queryCard=$mysqli->query("SELECT msgtxt FROM `httplog2` WHERE `openid`='".$list['openid']."' limit 0,1 ");
	        if($queryCard->num_rows>0){
	            $getCard=$queryCard->fetch_array();
	            $getTxt=trim($getCard['msgtxt']); 
	            $getTxt=trimall($getTxt);
	            $getTxt2=json_decode($getTxt, true);
	            if (isset($getTxt2['user'])) {
             	$cardNo=$getTxt2['user']['card_number'];
             }
             if (isset($getTxt2['card_number'])) {
             	$cardNo=$getTxt2['card_number'];
             }
	        }else{
                $queryCard=null;
	            $queryCard=$mysqli->query("SELECT msgtxt FROM `httplog3` WHERE `openid`='".$list['openid']."' limit 0,1 ");
		        if($queryCard->num_rows>0){
		            $getCard=$queryCard->fetch_array();
		            $getTxt=trim($getCard['msgtxt']); 
		            $getTxt=trimall($getTxt);
		            $getTxt2=json_decode($getTxt, true);
		           if (isset($getTxt2['user'])) {
             	$cardNo=$getTxt2['user']['card_number'];
             }
             if (isset($getTxt2['card_number'])) {
             	$cardNo=$getTxt2['card_number'];
             }
		            
		        }
	        }
        }

       if (!empty($cardNo)) {
          
           $mysqli->query("update  download set idcard='".trim($cardNo)."'   WHERE `openid`='".$list['openid']."'  ");
            
           
       }else{
       	$mysqli->query("update  download set idcard='seimsong'   WHERE `openid`='".$list['openid']."'  ");
       	echo $list['openid'];
       	echo "---end";
       }
       echo "$cardNo"; 
            echo '
			<script>
			window.setTimeout(function(){
			window.location.reload();
			}, 10);
			</script>
			     
			';

} 

 
 